package org.ofbiz.wvtools;

//import java.net.InetAddress;
//import java.net.UnknownHostException;
import java.util.Map;

import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilDateTime;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.ServiceUtil;

public class CoVisitLogServices {
    public static final String module = CoVisitLogServices.class.getName();

    public static Map<String, Object> createAutoVisitLog(DispatchContext ctx, Map<String, Object> context) {
	
	Map<String, Object> result = ServiceUtil.returnSuccess();
	String logTypeId = (String)context.get("logTypeId");
	Debug.log("saveSysSecurityPermissionToSecurityGroup   === start");
	Delegator delegator = ctx.getDelegator();
	GenericValue userLogin = (GenericValue) context.get("userLogin");
	try {
//	    InetAddress address = InetAddress.getLocalHost();
	    GenericValue person = delegator.findOne("Person", false, UtilMisc.toMap("partyId", userLogin.getString("partyId")));
	    GenericValue dataLog = delegator.makeValue("AutoVisitLog");
	    String id = delegator.getNextSeqId("AutoVisitLog");
	    dataLog.set("id", id);
	    dataLog.set("logTypeId", context.get("logTypeId"));
	    dataLog.set("logTypeDetailId", context.get("logTypeDetailId"));
	    dataLog.set("logTypeDetail", context.get("logTypeDetail"));
	    dataLog.set("objectId", context.get("objectId"));
	    
	    if(UtilValidate.isNotEmpty(context.get("content"))) {
		if(!logTypeId.equals("LOGIND")&&!logTypeId.equals("LOGOUTD")) {
		    dataLog.set("description", person.getString("firstName") + "" + context.get("content") + "的数据");
		}else {
		    dataLog.set("description", person.getString("firstName") + "" + context.get("content"));
		}
		
	    }
//	    dataLog.set("clientIp", "127.0.0.1");
//	    dataLog.set("serverIp", address.getHostAddress());
	    dataLog.set("firstName", person.getString("firstName"));
	    if(UtilValidate.isNotEmpty(person.getString("companyId"))) {
		dataLog.set("companyId", person.getString("companyId"));
	    }else {
		dataLog.set("companyId", "admin");
	    }
	    dataLog.set("createPartyId", userLogin.getString("partyId"));
	    dataLog.set("createUserId", userLogin.getString("userLoginId"));
	    dataLog.set("createTime", UtilDateTime.nowTimestamp());
	    dataLog.create();
	} catch (GenericEntityException e) {
	    Debug.logError(e.getMessage(), module);
	    return ServiceUtil.returnError("操作日志出错:" + e.getMessage());
	}
//	catch (UnknownHostException e) {
//	    Debug.logError(e.getMessage(), module);
//	    return ServiceUtil.returnError("IP解析出错:" + e.getMessage());
//	}
	return result;
    }
    

    
}
